//
//  LoginAccountViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/7.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "LoginAccountViewController.h"
#import "HomeViewController.h"
#import "SortViewController.h"
#import "GrowViewController.h"
#import "MyViewController.h"
#import "ForgetPasswordViewController.h"

@interface LoginAccountViewController ()
{
    UITextField * phone;
    UITextField * password;
}
@end

@implementation LoginAccountViewController

-(void)tapCancel
{
    [phone resignFirstResponder];
    [password resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backImage.image = [UIImage imageNamed:@"lb"];
    backImage.userInteractionEnabled = YES;
    [self.view addSubview:backImage];
    
    UITapGestureRecognizer * tapCancel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCancel)];
    [backImage addGestureRecognizer:tapCancel];
    
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 40, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UIButton * login = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth - 90, 20, 80, 40)];
    [login setTitle:@"忘记密码" forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15];
    [login setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [login addTarget:self action:@selector(forgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:login];
    
    UILabel * createAccount = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, ScreenWidth - 40, 30)];
    createAccount.font = [UIFont boldSystemFontOfSize:15];
    createAccount.text = @"登陆";
    [self.view addSubview:createAccount];
    
    phone = [[UITextField alloc]initWithFrame:CGRectMake(20, 170, ScreenWidth - 40, 30)];
    phone.placeholder = @"请输入手机账号";
    phone.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:phone];
    
    UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(20, 200, ScreenWidth - 40, 1)];
    line2.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:line2];
    
    password = [[UITextField alloc]initWithFrame:CGRectMake(20, 250, ScreenWidth - 40, 30)];
    password.placeholder = @"请输入密码";
    password.secureTextEntry = YES;
    [self.view addSubview:password];
    
    UIView * line3 = [[UIView alloc]initWithFrame:CGRectMake(20, 280, ScreenWidth - 40, 1)];
    line3.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:line3];
    
    
    UIButton * next = [[UIButton alloc]initWithFrame:CGRectMake(20, 320, ScreenWidth - 40, 40)];
    next.layer.borderColor = [UIColor orangeColor].CGColor;
    next.layer.borderWidth = 1;
    next.clipsToBounds = YES;
    next.layer.cornerRadius = 20;
    [next setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    [next addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:next];
    
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)nextAction
{
    [NetRequestCenter loginWithName:phone.text password:password.text result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            // success
            HomeViewController * home = [[HomeViewController alloc]init];
            SortViewController * sort = [[SortViewController alloc]init];
            GrowViewController * grow = [[GrowViewController alloc]init];
            MyViewController * my = [[MyViewController alloc]init];
            
            MMNavController * homen = [[MMNavController alloc]initWithRootViewController:home];
            homen.tabBarItem.title = @"首页";
            homen.tabBarItem.image = [[UIImage imageNamed:@"首页-normal"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            homen.tabBarItem.selectedImage = [[UIImage imageNamed:@"首页-highlight"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            MMNavController * sortn = [[MMNavController alloc]initWithRootViewController:sort];
            sortn.tabBarItem.title = @"分类";
            sortn.tabBarItem.image = [[UIImage imageNamed:@"分类-normal"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            sortn.tabBarItem.selectedImage = [[UIImage imageNamed:@"分类-highlight"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            MMNavController * grown = [[MMNavController alloc]initWithRootViewController:grow];
            grown.tabBarItem.title = @"成长";
            grown.tabBarItem.image = [[UIImage imageNamed:@"成长-normal"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            grown.tabBarItem.selectedImage = [[UIImage imageNamed:@"成长-highlight"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            MMNavController * myn = [[MMNavController alloc]initWithRootViewController:my];
            myn.tabBarItem.title = @"我的";
            myn.tabBarItem.image = [[UIImage imageNamed:@"我的-normal"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            myn.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的-highlight"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            UITabBarController * tabbar = [[UITabBarController alloc]init];
            tabbar.tabBar.tintColor = [UIColor redColor];
            tabbar.viewControllers = @[homen,sortn,grown,myn];
            tabbar.tabBar.tintColor = themeColor;
            
            [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
        }
        else
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[resultDic objectForKey:@"message"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *centain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:centain];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
   
}

-(void)forgetPassword
{
    ForgetPasswordViewController * forget = [[ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:forget animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
